#include <MyProject.h>

BLDC_PARA xdata BLDC;


// 电机启动参数初始化
void BL_S_ParaInit(void)
{
	    BLDC.BLDCSetFlag = 0;

}

/*---------------------------------------------------------------------------*/
/*  Name     :   void TIM1_Init(void)
    /* Input    :   NO
    /* Output   :   NO
    /* Description: Timer1的参数设置
    /*---------------------------------------------------------------------------*/
//uint16 tese1;
void TIM1_BLS_Init(void)
{	  	    
    
/*  -------------------------------------------------------------------------------------------------
TIM1_CR0
[7]    T1RWEN    对TIM1_CR0写0x81使能TIM1_CR0[T1RCEN]，写0x80不使能TIM1_CR0[T1RCEN]
[6:5]  T1CFLT    60度基准时间滤波选择  00-1个BCCR  01-2个BCCR 10-3个BCCR  11-4个BCCR
[4]    T1FORC    60度强制换相使能      0-->Disable  1-->Enable
[3:2]  T1OPS     换相触发信号选择      00-软件换相  01-重载定时器上溢触发 10-位置检测中断触发  11-RSV
[1]    T1BCEN    基本计数器的计数器使能
[0]    T1RCEN    对TIM1_CR0写0x81使能TIM1_CR0[T1RCEN]，写0x80不使能TIM1_CR0[T1RCEN]
-------------------------------------------------------------------------------------------------*/
	SetBit(TIM1_CR0 , T1CFLT1);	
	SetBit(TIM1_CR0 , T1CFLT0);	        

	ClrBit(TIM1_CR0 , T1FORC);	  

	ClrBit(TIM1_CR0 , T1OPS1);	
	ClrBit(TIM1_CR0 , T1OPS0);	   
    
/*  -------------------------------------------------------------------------------------------------
TIM1_CR1
[7]    T1BAPE    TIM1__BARR自动装载使能    
[6:5]  BSEL      屏蔽续流角度选择
-------------------------------------------------------------------------------------------------*/
    TIM1_CR1 = 0x40;	

/*  -------------------------------------------------------------------------------------------------
TIM1_CR2
[7]    T1BRS     基本计数器复位源选择    
[6:5]  CSEL      换相角度选择
-------------------------------------------------------------------------------------------------*/
    TIM1_CR2 = 0x01;	

 
/*-------------------------------------------------------------------------------------------------
Tim1_CR3
[7]    保留
[6:4]  T1PSC    定时器时钟分频选择
[3:2]  T1TIS    输入源（TI0/TI1/TI2）选择
                 00：GPIO作为输入，根据CMP_CR1[7]选择（P1.4/P1.6/P2.1）还是（P0.2/P3.7/P3.6），CMP_SR的结果通过GPIO产生
                 01：比较器（CMP0/CMP1/CMP2）的输出作为输入，CMP_SR的结果通过CMP产生
                 10：ADC
                 11：比较器（CMP0/CMP1/CMP2）&ADC	比较器优先级高
[1:0]  T1INM    输入TI0/TI1/TI2噪声脉宽选择
                 CMP_CR4[FAEN] = 0  00: 不滤波           01：8个时钟周期滤波   10：32个时钟周期滤波  11：64个时钟周期滤波
                 CMP_CR4[FAEN] = 1  00:32个时钟周期滤波  01：64个时钟周期滤波  10：96个时钟周期滤波  11：128个时钟周期滤波
-------------------------------------------------------------------------------------------------*/   
    //
    ClrBit(TIM1_CR3, T1PSC2);
    SetBit(TIM1_CR3, T1PSC1);
    ClrBit(TIM1_CR3, T1PSC0);
    
    
    //输入来源为CMP 
    ClrBit(TIM1_CR3, T1TIS1);
    SetBit(TIM1_CR3, T1TIS0);
    
    
    //TI2噪声脉宽选择
    SetBit(TIM1_CR3, T1INM1);
    SetBit(TIM1_CR3, T1INM0);  

/*-------------------------------------------------------------------------------------------------
TIM1_CR5           
[7]      T1POP       Driver计数器上溢事件触发数据传输使能  0-->Disable  1-->Enable
[6]      T1WTS       PWM OFF时候换相使能，消除窄脉冲       0-->Disable  1-->Enable
[5:4]    RSV
[3]      ITRIP_DIS   方波忽略ITRIP电流采样使能             0-->Disable  1-->Enable
[2]      UCOP_DIS    方波忽略导通相电压采样使能            0-->Disable  1-->Enable
[1:0]    T1AFL       ADC 采样电压计算结果滤波次数          00-1  01-2 10-4  11-8

-------------------------------------------------------------------------------------------------*/
//    TIM1_CR5 = 0x00;
    
/*-------------------------------------------------------------------------------------------------
TIM1_IER            TIMER1中断控制
[7]      T1UPD （当OPS=00时，软件对UPD写一触发数据传输）
[6]      保留 
[5]      保留
[4]      T1BOIE    0：禁止基本定时器上溢中断  1：使能基本定时器上溢中断
[3]      T1ROIE    0: 禁止重载定时器上溢中断  1: 使能重载定时器上溢中断
[2]      T1WTIE    0: 禁止写入时序中断        1: 使能写入时序中断
[1]      T1PDIE    0: 禁止位置检测中断        1: 使能位置检测中断          (过零点检查)
[0]      T1BDIE    0: 禁止屏蔽续流结束中断    1: 使能屏蔽续流结束中断
-------------------------------------------------------------------------------------------------*/
    TIM1_IER = 0x00;
    
/*-------------------------------------------------------------------------------------------------
TIM1_SR            TIMER1中断标志位
[7]      保留
[6]      保留 
[5]      T1ADIF    0: ADC中断标志位            
[4]      T1BOIE    0：基本定时器上溢中断  
[3]      T1ROIE    0: 重载定时器上溢中断标记
[2]      T1WTIF    0: 写入时序中断标记
[1]      T1PDIF    0: 位置检测中断标记          (过零点检查)
[0]      T1BDIF    0: 屏蔽续流结束中断标记
-------------------------------------------------------------------------------------------------*/
    TIM1_SR = 0x00;

		
		TIM1_DBR1 = UH_PWM_COM_VL_ON + CMP_W_DO;
    TIM1_DBR2 = UH_PWM_COM_WL_ON + CMP_V_UP;
    TIM1_DBR3 = VH_PWM_COM_WL_ON + CMP_U_DO;
    TIM1_DBR4 = VH_PWM_COM_UL_ON + CMP_W_UP;
    TIM1_DBR5 = WH_PWM_COM_UL_ON + CMP_V_DO;
    TIM1_DBR6 = WH_PWM_COM_VL_ON + CMP_U_UP;
		
    TIM1_DBR7 = UVW_OFF + CMP_UVW_ALL;	
    

    TIM1_CR4 = 1;
   
    
    TIM1_BCOR   = 65000;
    TIM1__BCNTR = 0;
    TIM1__BCCR  = 65500;
    TIM1__BARR  = 65500;
    TIM1__RARR  = 65500;
    TIM1__RCNTR = 0;
    
    SetBit(TIM1_CR0 , T1BCEN | T1RWEN | T1RCEN);	//基本定时器的计数器使能
}



/*---------------------------------------------------------------------------*/
/*  Name     :   void TIM1_Init(void)
    /* Input    :   NO
    /* Output   :   NO
    /* Description: Timer1的参数设置
    /*---------------------------------------------------------------------------*/

void TIM1_TO_Hardware(void)
{

	
    SetBit(TIM1_IER, T1PDIE); // 位置检测中断
    PTIM11 = 1;
    PTIM10 = 0;
    SetBit(TIM1_CR0, T1OPS1); //
    ClrBit(TIM1_CR0, T1OPS0);
    SetBit(TIM1_CR2, T1BRS); //强拖阶段，基本定时器按照软件换向之间的时间计数
}

// 启动算法
void BL_S_Function()
{      	
	// 当前处于启动阶段
   if( BLDC.PWM_COUNT < 65535 )
        {
            BLDC.PWM_COUNT++;
        }
    if (BLDC.BLDCSetFlag == 1)
    {
			
			  if( (BLDC.BDIF_Flag == 0) && ( BLDC.PWM_COUNT > MaskCount ) )          // 软件续流设置
        {    
          BLDC.BDIF_Flag = 1;  
        } 				
			  else if(BLDC.BDIF_Flag == 1)
				{

					 if(BLDC.PWM_COUNT <= ExchangeTime_ms)                              //强制换相时间判断
           { 			               						 
							 switch (TIM1_CR4)
							{
                case 1: 
									      BLDC.Level = CMP_SR & CMP2OUT; 
                        BLDC.Level = BLDC.Level>>2;
                       break;
                
                case 2: BLDC.Level = CMP_SR & CMP1OUT;
                       BLDC.Level = BLDC.Level>>1;
                       break;
                
                case 3: BLDC.Level = CMP_SR & CMP0OUT; 
                       break;
                
                case 4: BLDC.Level = CMP_SR & CMP2OUT;								
                        BLDC.Level = BLDC.Level>>2;
                        break;
                
                case 5: BLDC.Level = CMP_SR & CMP1OUT;
                        BLDC.Level = BLDC.Level>>1;
                        break;
                
                case 6: BLDC.Level = CMP_SR & CMP0OUT; 
                        break;
               } 
 
									 //BEMF信号滤波
									if(
										(   (((  TIM1_CR4 & 0x01)  && (BLDC.Level == 0))  //135  下降沿      正转
										 || ((!(TIM1_CR4 & 0x01)) && (BLDC.Level == 1))   //246  上升沿
										)&&( FR == CW )) ||
									  (   ((  TIM1_CR4 & 0x01)  && (BLDC.Level == 1))   //135  上升沿      反转
                   || (((!(TIM1_CR4 & 0x01)) && (BLDC.Level == 0))    //246  下降沿 
                  )&&( FR == CCW ))
									)
									{

											BLDC.Edge_Flt_cnt++;
									} 
									else       
									{
											BLDC.Edge_Flt_cnt = 0;
									}  
									
									 //确认到达BEMF的过零点
                   if( ( BLDC.Edge_Flt_cnt >= 5)  )//&&(BLDC.PWM_COUNT >= 20)
                   {
										 
										
											/*软件换相*/
											SetBit(TIM1_IER, T1UPD);
//											BLDC.COM_Flag = 1;
											BLDC.Edge_Flt_cnt = 0;     
											
											/*换相次数处理*/
											if( BLDC.COM_COUNT   <= 65535)     BLDC.COM_COUNT++;          
											if( BLDC.Right_COUNT <= 65535)     BLDC.Right_COUNT++;        
											if( BLDC.Right_COUNT > 6 )         BLDC.Force_COUNT=0;        
											
											/*根据载波计算转速*/
											BLDC.PWM_COUNT_LAST = BLDC.PWM_COUNT; 
//											PWMCount_LPF();
											BLDC.PWM_COUNT = 0;
											
											/*换相后的标志位处理*/
											BLDC.BDIF_Flag = 0; 
										 
                       if( FR == CW )		
											 {												 
													if (TIM1_CR4 & 0x01)          BLDC.Level = 1;
													else                          BLDC.Level = 0; 
											 }
											 else
											 {
												  if (TIM1_CR4 & 0x01)          BLDC.Level = 0;
                          else                          BLDC.Level = 1; 
											 }
											
											
											/*切换为硬件运行*/   
											if(    (BLDC.Right_COUNT    > 36 )                             
											)
												{
													 
													mcState = mcRun;
													TIM1_TO_Hardware();

													BLDC.BLDCSetFlag = 2;    //切换至硬件运行状态

													BLDC.BDIF_Flag    = 0;
//													BLDC.COM_Flag     = 0;
													BLDC.Level        = 0;
													BLDC.Edge_Flt_cnt = 0;
													BLDC.COM_COUNT    = 0;
													BLDC.Right_COUNT  = 0;
													BLDC.Force_COUNT  = 0;
//													BLDC.ON_OFF_FLAG  = 0;
											 }     
                   }    														
							 								 
				}				
				else
				{
					if( FR == CW )
           {              
                SetBit(TIM1_IER, T1UPD);         
                if (TIM1_CR4 & 0x01)          BLDC.Level = 1;
                else                          BLDC.Level = 0;
           }
           else if ( FR == CCW)
           {
                SetBit(TIM1_IER, T1UPD);
                if (TIM1_CR4 & 0x01)          BLDC.Level = 0;
                else                          BLDC.Level = 1;
           }
           
         
           if( BLDC.COM_COUNT   <= 65535)    BLDC.COM_COUNT++;     
           BLDC.Right_COUNT = 0;                                  
           if( BLDC.Force_COUNT <= 65535 )   BLDC.Force_COUNT++;   
           
           
           
           BLDC.PWM_COUNT_LAST = BLDC.PWM_COUNT; 
//           PWMCount_LPF();
           BLDC.PWM_COUNT = 0;
           
//           BLDC.COM_Flag     = 1;
           BLDC.Edge_Flt_cnt = 0;
           BLDC.BDIF_Flag    = 0;      
				}
					
		  }
	}
}
    


